package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.xml.bind.JAXBException;

import to.MaterialTO;

public class MaterialParser {
	private static MaterialParser instance;
	private boolean key;
	
	private MaterialParser(){
		key = true;
	}
	
	public static MaterialParser getInstance() {
		if(instance==null){
			instance = new MaterialParser();
		}
		while(!instance.key){
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}		
		instance.key = false;
		return instance;
	}
	
	public void returnKey(){
		key = true;
	}
	
	/*public void writeMaterial(MaterialTO material){
		JAXBContext context = null;
		Marshaller marshaller = null;
		
		try {
			context = JAXBContext.newInstance(MaterialTO.class);
			marshaller = context.createMarshaller();
			marshaller.setProperty(marshaller.JAXB_ENCODING, "utf-8");
			marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.marshal(material, new FileWriter("C:\\cloth-factory\\"+material.getMaterialId()+".xml"));
		} catch (JAXBException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public MaterialTO readMaterial(String materialId){
		MaterialTO material = null;
		
		JAXBContext context = null;
		Unmarshaller unmarshaller = null;
		
		try {
			context = JAXBContext.newInstance(MaterialTO.class);
			unmarshaller = context.createUnmarshaller();
			material = (MaterialTO) unmarshaller.unmarshal(new FileReader("C:\\cloth-factory\\"+materialId+".xml"));
			
		} catch (JAXBException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			material = new MaterialTO();
		}
		
		return material;
	}*/
	
	public void writeMaterial(MaterialTO material){
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		File file = new File("C:\\cloth-factory\\"+material.getMaterialId()+".mt");
		
		try {
			if(file.exists())
				file.delete();
			fos = new FileOutputStream(file);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(material);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(oos!=null){
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public MaterialTO readMaterial(String materialId){
		MaterialTO material = null;
		ObjectInputStream oin = null;
		FileInputStream fin = null;
		File file = new File("C:\\cloth-factory\\"+materialId+".mt");
		
		if(!file.exists()){
			material = new MaterialTO();
			material.setMaterialId(materialId);
			return material;
		}
		try {
			fin = new FileInputStream(file);
			oin = new ObjectInputStream(fin);
			material = (MaterialTO) oin.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	finally{
			if(oin!=null){
				try {
					oin.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return material;
	}
}
